const { Controller } = require('egg')
const Response = require('../utils/Response')
function isUndefined(ctx, arr) {
  let isUndefined = false
  let arr1 = [...arr]
  let str = "缺少必填字段"
  let number = 0
  arr1.forEach((item, index) => {

    if (Object.values(item)[0] == undefined || Object.values(item)[0] === "") {
      number++;
      str = str + Object.keys(item)[0] + "、"
    }
  })
  if (number > 0) {
    ctx.body = new Response(400, str, "")
    isUndefined = true
  }
  return isUndefined
}
/**
 * @Controller  预约模块(核心)
 */
class ReservationController extends Controller {
  // 发起预约
  /**
* @Router post /res/create-res
* @Summary 预约实验室
* @Description dayTimeId(当前日期可预约时间段id)、userId、type(预约类型课堂或个人)、isExamine(是否需要人工审核)为必传字段
* @Request body createRes
* @apikey
*/
  async createRes() {

    let { ctx, service } = this
    let { dayTimeId, userId, askReason, type, isExamine } = ctx.request.body
    // console.log(userId);
    let resUn = isUndefined(ctx, [{ dayTimeId }, { userId }, { type }, { isExamine }])
    if (!askReason) {
      askReason = ""
    }
    if (!resUn) {
      let res = await service.reservation.createRes(dayTimeId, userId, askReason, type, isExamine)
      if (res == "预约申请成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }

    }
  }
  // 审核预约
  /**
* @Router post /res/check-res
* @Summary 审核预约
* @Description reservationId 预约记录Id 必传, rejectReason 拒绝原因 非必传, checkStatus 0为通过 1为不通过 必传字段,status 当前状态 必传字段
* @Request body checkRes
* @apikey
*/
  async checkRes() {
    let { ctx, service } = this
    let { reservationId, rejectReason, checkStatus, status } = ctx.request.body
    if (!rejectReason) {
      rejectReason = ""
    }
    let isUn = isUndefined(ctx, [{ reservationId }, { checkStatus }, { status }])
    if (!isUn) {
      if (status != 1) {
        ctx.body = new Response(403, "预约数据状态错误", "")
        return

      }
      reservationId = parseInt(reservationId)
      let res = await service.reservation.checkRes(reservationId, rejectReason, checkStatus)
      if (res == "审核成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }
    }

  }
  // 先判断时间冲突
  // 判断时间时间冲突返回true代表没有时间冲突，返回false代表有时间冲突
  /**
* @Router get /res/noConflict
* @Summary 判断时间冲突
* @Description userId 用户Id, start 要预约的开始时间, end 要预约的结束时间, dateTime 当前日期，返回true代表没有时间冲突，返回false代表有时间冲突
* @Request query string *userId
* @Request query string *start
* @Request query string *end
* @Request query string *dateTime
* @apikey
*/
  async noConflict() {
    let { ctx, service } = this
    // 用户id 要预约的开始时间 要预约的结束时间 当前日期
    let { userId, start, end, dateTime } = ctx.query
    let isUn = isUndefined(ctx, [{ userId }, { start }, { end }, { dateTime }])
    if (!isUn) {
      let res = await service.reservation.noConflict(userId, start, end, dateTime)
      ctx.body = new Response(200, res, "")
    }
  }
  // 判断当日的当前时间段的该实验室的同类别实验室，是否有没有预约满的，有的话就返回那个dayTimeId ,canRes返回false，表示当前暂时不可预约
  // 剩余空间合理利用
  /**
* @Router get /res/canIRes
* @Summary 判断当日当前可预约时间段是否有相同类型的实验室有剩余空间（返回true可以继续预约当前实验室，返回false加timeId得跳转到那个实验室）
* @Description tStatus 当前预约的可预约信息的状态, typeId 实验室类型, resType 发起预约的类型, dateTime 当日时间, start 开始时间, end 结束时间
* @Request query number *tStatus
* @Request query number *typeId
* @Request query number *resType
* @Request query string *dateTime
* @Request query string *start
* @Request query string *end
* @apikey
*/
  async canIRes() {
    let { ctx, service } = this
    // 前端得判断，如果没有被预约，就可以两个都选，被课堂预约了谁也不能选，被个人预约了只能有个人预约一个选项
    // 哪一个时间段，哪一种类型，如果类型不同，是在前端不给选
    // 如果传过来的tStatus表示课堂预约，应该不可能，只要是课堂预约就不能预约了，是个人预约也不能选课堂预约
    // tStatus 0 没有被预约 tStatus 1 课堂 tStatus 2个人
    // 当前时间段的剩余容量，小于实验室容量 视为没有被个人预约占完
    // resType 0是课堂 1是个人
    let { tStatus, typeId, resType, dateTime, start, end } = ctx.query
    let res1 = isUndefined(ctx, [{ tStatus }, { typeId }, { resType }, { dateTime }, { start }, { end }])
    // console.log(start,end);
    if (!res1) {
      let res = await service.reservation.canIRes(tStatus, typeId, resType, dateTime, start, end)
      ctx.body = new Response(200, res, "")
    }
  }

  // 取消预约
  /**
* @Router get /res/cancle-res
* @Summary 取消预约
* @Description 必传字段reservationId(预约记录id)、status(预约记录的当前状态) 只能传 1待审核 或 2 审核通过
* @Request query number *reservationId
* @Request query number *status
* @apikey
*/
  async cancleRes() {
    let { ctx, service } = this
    let { reservationId, status } = ctx.query
    let isUn = isUndefined(ctx, [{ reservationId }, { status }])

    if (!isUn) {
      if (status == 1 || status == 2) {
        let res = await service.reservation.cancleRes(reservationId, status)
        if (res == "取消成功") {
          ctx.body = new Response(200, res, "")
        } else {
          ctx.body = new Response(403, res, "")
        }
      } else {
        ctx.body = new Response(403, "请求状态错误,只能是待审核或通过状态", "")
      }

    }
  }

  // 获取预约详情
  /**
* @Router get /res/detail-res
* @Summary 获取预约详情
* @Description 必传字段为 resverationId 预约id
* @Request query number *reservationId
* @apikey
*/
  async getResDetail() {
    let { ctx, service } = this
    let { reservationId } = ctx.query
    let isUn = isUndefined(ctx, [{ reservationId }])
    if (!isUn) {
      let res = await service.reservation.getResDetail(reservationId)
      if (res.message == "获取成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")

      }
    }
  }

  /**
  * @Router get /res/res-list
  * @Summary 条件获取预约列表
  * @Description 可查询条件 dateTime日期 、status 状态 、type类型 0课堂 1个人 、isExamine审核方式
  * @Request query string dateTime
  * @Request query number status
  * @Request query number type
  * @Request query number isExamine
  * @Request query number reservationId
  * @Request query string labName
  * @apikey
  */
  // 条件获取预约列表
  async getResList() {
    let { ctx, service } = this
    //  根据日期、状态、类型、审核方式来获取
    let { dateTime, status, type, isExamine,labName,reservationId } = ctx.query
    let res = await service.reservation.getResList(dateTime, status, type, isExamine,labName,reservationId)
    ctx.body = new Response(200, res, "")
  }
}






module.exports = ReservationController